package search;

import java.io.File;

import act.IActor;
import act.End;

public class Dir
{

   public static void dir(String rootDir, String fileNamePattern,  boolean deep, IActor out)
   {
       File root = new File(rootDir);
       if(!root.isDirectory())
       {
      	 System.out.println(root.getAbsolutePath() + "is not a directory");
       }
       else
       {
    	   dir(root, fileNamePattern,  deep, out);    	   
       }
       out.send(new End());
	   
   }
   
   private static void dir(File dir, String fileNamePattern,  boolean deep, IActor out)
   {
       File[] list = dir.listFiles();       
       if(list != null)
       {
      	 for(File file: list)
      	 {
      		 if(!file.isDirectory())
      		 {
       			if(out != null && file.getName().matches(fileNamePattern))
      				out.send(file.getAbsolutePath());
      		 }
      		 else if(deep)
      		 {
       			dir(file, fileNamePattern,  deep, out);
      		 }
      	 }
       }	   
   }


}

 